"""

"""

from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.app import App
from ..components.result_viewer import ResultViewer

class ResultWindow(Screen):
    """"""
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._build_ui()
    
    def _build_ui(self):
        """"""
        main_layout = BoxLayout(
            orientation='vertical',
            spacing=20,
            padding=20
        )
        
        # 
        title = Label(
            text="Processing Results",
            font_size=48,  # 放大两倍字体
            size_hint_y=None,
            height=80,
            bold=True,
            color=(0.2, 0.6, 0.9, 1)
        )
        main_layout.add_widget(title)
        
        # 
        self.result_viewer = ResultViewer()
        main_layout.add_widget(self.result_viewer)
        
        # 
        button_layout = BoxLayout(
            orientation='horizontal',
            size_hint_y=None,
            height=50,
            spacing=10
        )
        
        # 
        home_btn = Button(
            text=" Home",
            size_hint_x=0.3
        )
        home_btn.bind(on_press=self._go_home)
        button_layout.add_widget(home_btn)
        
        # 
        new_btn = Button(
            text="Process New Video",
            size_hint_x=0.4,
            font_size=24,  # 放大字体
            height=60
        )
        new_btn.bind(on_press=self._process_new)
        button_layout.add_widget(new_btn)
        
        # 
        settings_btn = Button(
            text="Settings",
            size_hint_x=0.3,
            font_size=24,  # 放大字体
            height=60
        )
        settings_btn.bind(on_press=self._open_settings)
        button_layout.add_widget(settings_btn)
        
        main_layout.add_widget(button_layout)
        
        self.add_widget(main_layout)
    
    def on_enter(self):
        """"""
        app = App.get_running_app()
        
        # 
        if hasattr(app, 'result_data') and app.result_data:
            self.result_viewer.update_result(app.result_data)
        else:
            # 
            self._show_sample_data()
    
    def _show_sample_data(self):
        """"""
        sample_data = {
            'transcript': {
                'text': 'This is a sample transcript text. In this video, we discussed the development history of artificial intelligence, from early expert systems to modern deep learning technologies. We also explored AI applications in various fields, including natural language processing, computer vision, and robotics.',
                'language': 'Chinese',
                'segments': []
            },
            'summary': {
                'summary': 'This video mainly introduces the development history and application fields of artificial intelligence.\n\nMain content includes:\n1. AI development history: from expert systems to deep learning\n2. Core technologies: natural language processing, computer vision\n3. Application fields: robotics and other aspects\n\nThe video provides viewers with a comprehensive overview of AI technology.',
                'keywords': ['artificial intelligence', 'deep learning', 'natural language processing', 'computer vision', 'robotics', 'expert systems'],
                'chapters': [
                    {
                        'title': 'Introduction',
                        'start_time': 0,
                        'end_time': 60,
                        'content': 'Introducing the video topic and basic concepts of AI'
                    },
                    {
                        'title': 'AI Development History',
                        'start_time': 60,
                        'end_time': 180,
                        'content': 'The development process from expert systems to modern deep learning'
                    },
                    {
                        'title': 'Application Fields',
                        'start_time': 180,
                        'end_time': 300,
                        'content': 'Specific application cases of AI in various fields'
                    }
                ]
            },
            'processing_time': '2 minutes 30 seconds'
        }
        
        self.result_viewer.update_result(sample_data)
    
    def _go_home(self, instance):
        """"""
        app = App.get_running_app()
        app.navigate_to('main')
    
    def _process_new(self, instance):
        """"""
        app = App.get_running_app()
        app.navigate_to('upload')
    
    def _open_settings(self, instance):
        """"""
        app = App.get_running_app()
        app.navigate_to('settings')